﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IDAL;
using SD.Entities;
using System.Data.SqlClient;
using System.Data;

namespace SD.DAL
{
    public class CountryDAO : BaseDAO, ICountryDAO
    {
        #region ICountryDAO Members

        /// <summary>
        /// Get all country
        /// </summary>
        /// <returns></returns>
        public List<Country> GetAll()
        {
            // Obtain SqlCommand object
            SqlCommand comm = CreateCommand();

            // Assign stored procedure
            comm.CommandText = "CountryGetAll";

            // Execute stored procedure
            DataTable dt = ExecuteSelectCommand(comm);

            // Create new list country
            List<Country> list = new List<Country>();

            // add each country in table to list
            foreach (DataRow row in dt.Rows)
            {
                Country c = new Country();
                c.CountryID = row["CountryID"].ToString();
                c.Name = row["Name"].ToString();
                list.Add(c);
            }

            // return list countries
            return list;
        }

        /// <summary>
        /// Get country by id
        /// </summary>
        /// <param name="countryId"></param>
        /// <returns></returns>
        public Country GetById(string countryId)
        {
            // Obtain SqlCommand object
            SqlCommand comm = CreateCommand();

            // Assign stored procedure
            comm.CommandText = "CountryGetById";

            // Add parameters to stored procedure
            comm.Parameters.AddWithValue("@CountryID", countryId);

            // Execute stored procedure
            DataTable dt = ExecuteSelectCommand(comm);

            Country c = null;

            // if table has data
            if (dt.Rows.Count > 0)
            {
                c = new Country();
                c.CountryID = dt.Rows[0]["CountryID"].ToString();
                c.Name = dt.Rows[0]["Name"].ToString();                
            }

            // return country
            return c;
        }

        /// <summary>
        /// Insert new country
        /// </summary>
        /// <param name="country"></param>
        /// <returns></returns>
        public bool Insert(Country country)
        {
            // Obtain SqlCommand object
            SqlCommand comm = CreateCommand();

            // Assign stored procedure
            comm.CommandText = "CountryInsert";

            // Add parameters to stored procedure
            comm.Parameters.AddWithValue("@Name", country.Name);

            int result = -1;
            try
            {
                // Execute stored procedure
                result = ExecuteNonQuery(comm);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // return true if insert successfully, otherwise return false
            return (result != -1);

        }

        /// <summary>
        /// Update country
        /// </summary>
        /// <param name="country"></param>
        /// <returns></returns>
        public bool Update(Country country)
        {
            return false;
        }


        /// <summary>
        /// Delete country
        /// </summary>
        /// <param name="countryId"></param>
        /// <returns></returns>
        public bool Delete(string countryId)
        {
            return false;
        }

        #endregion
    }
}
